import numpy as np
import requests
import  os
import json
import base64
capurl = 'https://cas.gzhu.edu.cn/cas_server/captcha.jsp'

def get_captcha_code(session: requests.Session, overlay=False, api_key='helloworld', language='eng')->str:
    payload = {'isOverlayRequired': overlay,
               'apikey': api_key,
               'language': language,
               'scale': True,
               'OCREngine': 2
               }
    with open("captcha.png", 'wb') as f:
        f.write(session.get(capurl).content)
    with open("captcha.png", 'rb') as f:
        r = requests.post('https://api.ocr.space/parse/image',
                          files={"captcha.png": f},
                          data=payload,
                          )
    return json.loads(r.content.decode())["ParsedResults"][0]["ParsedText"]
def ocr_space_file(filename, overlay=False, api_key='helloworld', language='eng'):
    payload = {'isOverlayRequired': overlay,
               'apikey': api_key,
               'language': language,
               'scale': True,
               'OCREngine': 2
               }
    with open(filename, 'rb') as f:
        r = requests.post('https://api.ocr.space/parse/image',
                          files={filename: f},
                          data=payload,
                          )
    content = json.loads(r.content.decode())
    return content
session = requests.Session()
print(get_captcha_code(session))
# re = ocr_space_file("captcha.png")
# print(type(re))
# print(re)
# with open("captcha.jsp", "rb")as f:
#     print(f.read())